import { reqGoodsInfo, reqAddOrUpdateCart } from '@/api'
//封装游客身份模块uuid-->生成一个随机字符串(不能再变)
import { getUUID } from '@/utils/uuid_token'
const state = {
  //产品信息
  goodsInfo: {},
  //游客临时身份
  uuid_token:getUUID()
}
const mutations = {
  //获取产品信息
  GETGOODSINFO(state, goodsInfo) {
    state.goodsInfo = goodsInfo
  }
}
const actions = {
  //获取产品信息
  async getGoodsInfo({ commit }, skuId) {
    let result = await reqGoodsInfo(skuId)
    if (result.code == 200) {
      commit('GETGOODSINFO', result.data)
    }
  },
  //添加或更新购物车
  async addOrUpdateCart({ commit }, { skuId, skuNum }) {
    let result = await reqAddOrUpdateCart(skuId, skuNum)
    //发请求:前端带一些参数给服务器【需要存储这些数据】，存储成功了，没有给返回数据
    //不需要在三连环（仓库存储数据了）
    //注意:async函数执行返回的结果一定是一个promise【要么成功，要么失败】
    if (result.code == 200) {
      return 'ok'
    } else {
      //返回的是失败的标记
      return Promise.reject(new Error('fail'))
    }
  }
}
const getters = {
  categoryView(state) {
    return state.goodsInfo.categoryView || {}
  },
  skuInfo(state) {
    return state.goodsInfo.skuInfo || {}
  },
  spuSaleAttrList(state) {
    return state.goodsInfo.spuSaleAttrList || []
  }
}

export default {
  state,
  mutations,
  actions,
  getters
}
